package interviewClassic150;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class IsIsomorphic {
    public boolean isIsomorphic(String s, String t) {
        //定义两个个map，双重映射，映射s和t的每个字母的映射关系
        Map<Character,Character> mapST = new HashMap<>();
        Map<Character,Character> mapTS = new HashMap<>();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char sChar = s.charAt(i);
            char tChar = t.charAt(i);
            //如果不存在这个映射就加入这个映射，如果存在这个映射则判断现在的映射与之前的映射是否一致
            if (!mapST.containsKey(sChar)) mapST.put(sChar,tChar);
            else {
                char sTot = mapST.get(sChar);
                if (sTot != tChar) return false;
            }

            if (!mapTS.containsKey(tChar)) mapTS.put(tChar,sChar);
            else {
                char tTos = mapTS.get(tChar);
                if (tTos != sChar) return false;
            }
        }

        return true;
    }

    @Test
    public void test() {
        String s = "badc";
        String t = "baba";
        boolean isomorphic = isIsomorphic(s, t);
        System.out.println(isomorphic);
    }
}
